home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13101 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  118 lines

  1. Path: info.uah.edu!oreo!gbacon
  2. From: gbacon@oreo (Greg Bacon)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please HELP with struct and linked list
  5. Date: 4 Apr 1996 17:18:40 GMT
  6. Organization: The University of Alabama in Huntsville
  7. Message-ID: <4k109g$6mv@info.uah.edu>
  8. References: <31630C5A.53E6@soleil.acomp.usf.edu>
  9. Reply-To: gbacon@CS.UAH.Edu
  10. NNTP-Posting-Host: oreo.aspire.cs.uah.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Chris Frenck (cfrenck@soleil.acomp.usf.edu) wrote:
  14. : Could anyone PLEASE help me. I am having a great deal of difficulty 
  15. : understanding the concepts of the following.
  16.  
  17. : struct Class
  18. : {
  19. :   char prefix;
  20. :   char number;
  21. :   char instructor;
  22. :   char grade;
  23. :   int credits;
  24. :   struct Class *next; /*???*/
  25. : };
  26.  
  27. : struct student
  28. : {
  29. :   char fname;
  30. :   char lname;
  31. :   struct Record *next *previous;
  32. :   struct Class *next_class; /*???*/
  33. : };
  34.  
  35. : The two main areas I am having extreme difficulties in are forming a
  36. : linked list of students, sorting the list alphabetically by last name.
  37. : I also need to add more than one class per student. If anyone could help
  38. : me get over this hurdle I would be very happy.
  39.  
  40. : I am having problems with the syntax of just how to do it. Does anyone
  41. : have an example of a program that does something similar? Or could someone
  42. : give me a "skeleton" of how to do it????
  43.  
  44. : I have to do many other things with this program, but cannot continue
  45. : until I figure this problem out.
  46.  
  47. : PLEASE help an honest, hard-working college student out!
  48.  
  49. : Sincerely,
  50.  
  51. : Chris Frenck
  52. : cfrenck@soleil.acomp.usf.edu
  53.  
  54. OK.. this is how I'm interpreting your spec.  You want something like a
  55. schedule for each student (with the list of students sorted alphabetically)
  56. cam sa:
  57.  
  58. Student1 --> class list
  59.    |
  60. Student2 --> class list
  61.    :
  62. Student_{\lambda} --> class list
  63.  
  64.  
  65. One way to do that would be to have structs similar to these:
  66.  
  67. struct _class_tag
  68. {
  69.    char prefix[PREFIXLEN];
  70.    char number[NUMBERLEN];
  71.    char instructor[INSTRUCTORLEN];
  72.    char grade;
  73.    int  credits;
  74.  
  75.    struct _class_tag *next;
  76. };
  77.  
  78. struct _student_tag
  79. {
  80.    char fname[NAMELEN];
  81.    char lname[NAMELEN];
  82.  
  83.    struct _class_tag *classes;
  84.    struct _student_tag *next;
  85. };
  86.  
  87. Note that an object of type char can hold only a single character.  I
  88. suspect you want fname, lname, et al to be arrays of char.  You'll
  89. want to use the preprocessor #define to define the values of the
  90. length constants ([A-Z][A-Z]*LEN), i.e.,
  91.  
  92. #define NAMELEN 15
  93.  
  94. That way, you avoid lots of calls to the allocator and grab it all
  95. in one shot (with a minor inefficiency in storage... ahh, the good ol'
  96. space-time tradeoff).
  97.  
  98. To save typing (no pun intended), you might consider a couple of
  99. typedefs, i.e.,
  100.  
  101. typedef struct _class_tag Class;
  102. typedef struct _student_tag Student;
  103.  
  104. If your compiler compiles C and C++, be careful about using 'class' (note
  105. the case); the compiler is likely to get horribly confused.
  106.  
  107. From there, your task is just to maintain your list of students and
  108. append classes to the proper student.  I have no idea what you were
  109. trying to do with 'struct Record'.  If it had some significance, you
  110. didn't make it clear.
  111.  
  112. Hope this helps,
  113. Greg
  114. --
  115. Greg Bacon <gbacon@cs.uah.edu>
  116. University of Alabama in Huntsville
  117. CS Department Systems Support Team
  118.